package com.zfy.tingshu.album.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfy.tingshu.model.album.TrackInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zfy.tingshu.vo.album.AlbumTrackListVo;
import com.zfy.tingshu.vo.album.TrackListVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.RequestParam;

@Mapper
public interface TrackInfoMapper extends BaseMapper<TrackInfo> {


    Page<TrackListVo> findUserTrackPage(Page<TrackListVo> trackInfoPage, @RequestParam("userId") Long userId);

    @Update("update track_info set order_num = order_num -1 where album_id = #{albumId} and order_num > #{orderNum} and is_deleted = 0")
    void updateOrderNum(@Param("albumId") Long albumId,@Param("orderNum") Integer orderNum);

    @Select("select track_info.id `track_id`,\n" +
            "       track_title,\n" +
            "       media_duration,\n" +
            "       order_num,\n" +
            "       track_info.create_time,\n" +
            "       max(case when track_stat.stat_type='0701' then track_stat.stat_num else 0 end) playStatNum,\n" +
            "       max(case when track_stat.stat_type='0704' then track_stat.stat_num else 0 end) commentStatNum\n" +
            "from track_info inner join track_stat on track_info.id=track_stat.track_id\n" +
            "where album_id =#{albumId} \n" +
            "group by track_stat.track_id")
    IPage<AlbumTrackListVo> selectAlbumTrackList(Page<AlbumTrackListVo> albumTrackListVoPage, @Param("albumId") Long albumId);

    @Update("update track_stat\n" +
            "        set stat_num = stat_num + #{count}\n" +
            "where track_id = #{trackId} and stat_type = #{statType}")
    void updateStat(@Param("trackId") Long trackId,@Param("statType") String statType,@Param("count") Integer count);
}
